package com.example.bootdemo.repository;

import com.example.bootdemo.pojo.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import java.util.List;

public interface UserRepository extends JpaRepository<UserInfo,Long> {

    UserInfo findUserInfoByUserName(String userName);

    UserInfo findUserInfoByTelNumEquals(Long telNum);

    UserInfo findUserInfoByEmail(String email);

    List<UserInfo> findUserInfosByUserNameLike(String userName);

    List<UserInfo> findUserInfosByUserNameCnLike(String userNameCn);

    @Query(value="select user_id,user_name,password,user_sex,user_name_cn,user_role,tel_num," +
            "email,reg_date,birth_day,created_by,created_time,last_updated_by,last_updated_time from user_info where user_role=?1",nativeQuery = true)
    List<UserInfo> findByUserRole(String userRole);

    @Query(value="select user_id,user_name,password,user_sex,user_name_cn,user_role,tel_num,email," +
            "reg_date,birth_day,created_by,created_time,last_updated_by,last_updated_time from user_info where user_name like ?1%",nativeQuery = true)
    List<UserInfo> findByLikeUserName(String userName);

    @Query(value="select user_id,user_name,password,user_sex,user_name_cn,user_role,tel_num,email," +
            "reg_date,birth_day,created_by,created_time,last_updated_by,last_updated_time from user_info where email=?1",nativeQuery = true)
    UserInfo findByEmail(String email);

    @Query(value="select * from user_info where user_name like ?1% order by ?2 desc",nativeQuery = true)
    List<UserInfo> findByUserNameAndOrderBy(String userName, String sort);

    @Modifying
    @Query(value="update user_info set email=?2 where user_name=?1",nativeQuery = true)
    Integer updateEmailByUserName(String username,String email);


}
